package dut.ir.lxb.weibo.main;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import dut.ir.lxb.weibo.bean.EventBean;
import dut.ir.lxb.weibo.bean.UserActionBean;
import dut.ir.lxb.weibo.bean.WeiboBean;
import dut.ir.lxb.weibo.db.DBConnectionManager;
import dut.ir.lxb.weibo.db.EventInfoDao;
import dut.ir.lxb.weibo.db.UserWeiboActionDao;
import dut.ir.lxb.weibo.db.WeiboInfoDao;
import dut.ir.lxb.weibo.io.WeiboInfoWriter;
import dut.ir.lxb.weibo.util.ActionType;

public class WeiboFormatter
{
	public static void main(String [] args)
	{
		/*
		EventInfoDao eventDao = EventInfoDao.getInstance();
		List<EventBean> eventList = eventDao.getEventListGroupbyTime("2014-11-24");
		
		
		
		UserWeiboActionDao actionDao = UserWeiboActionDao.getInstance();
		//获得交互最多的用户
		@SuppressWarnings("unused")
		List<UserActionBean> userList = actionDao.getTopkActionUsers("2244218960", ActionType.FORWARD, 1);
		WeiboInfoDao weiboDao = WeiboInfoDao.getInstance();
		//获得评论或点赞或转发最多的微博
		@SuppressWarnings("unused")
		List<WeiboBean> actionWeiboList = weiboDao.getTopkWeiboByActionType("2244218960", ActionType.FORWARD, 1);
		
		WeiboInfoDao weiboDao = WeiboInfoDao.getInstance();
		List<WeiboBean> weiboList = weiboDao.getAllWeiboList(false);
		
		WeiboInfoWriter  writer = new WeiboInfoWriter("./weibolist.xml");
		for(WeiboBean weibo : weiboList)
		{
			writer.writeBody(weibo.toXMLText());
			
		}
		writer.close();
		
		*/
		String timeStamp = "2014-12-05";
		if(args.length > 0)
			timeStamp = args[0];
		
		
		getNewWeiboList(timeStamp, false);
	}
	
	/**
	 * 获得某个时间点之后新发表的微博列表
	 * @param uid
	 * @param isEventOnly
	 * @param timeDesc
	 * @return
	 */
	public static boolean getNewWeiboList(String timeStamp, boolean isTimeIncreasing)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			WeiboInfoWriter  writer = new WeiboInfoWriter("./weibolist.xml");
			
			
			//String eventCondition = "";
			//if(isEventOnly)
			//	eventCondition = "and event is not null";
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_WeiboInfo where posttime is not null and unix_timestamp(downloadtime) >= unix_timestamp(?)";
			//sqlStr = String.format(sqlStr, eventCondition, order);
			
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, timeStamp);
			//List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("mid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("content"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				writer.writeBody(wb.toXMLText());
				System.out.println(String.format("正在写微博[%s]", wb.getMid()));
				
				//wbList.add(wb);
			}
			
			rs.close();
			ps.close();
			writer.close();
			
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			//return wbList;
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
		return true;
	}
}
